package demo1;

public class Sort {
    //插入排序
    //时间：逆序O(n^2),顺序O(n)
    //稳定
    public void insert(int[] array){
        for(int i = 1; i < array.length; i++){
            int key = array[i];
            int j = i - 1;
            for(; j >= 0; j--){
                if(array[j] > key){
                    array[j + 1] = array[j];
                }else{
                    break;
                }
            }
            array[j + 1] = key;
        }
    }
    //希尔排序
    //时间：O(n^1.3 ~ n^1.4)
    //不稳定
    public void shell(int[] array, int gap){
        for(int i = gap; i < array.length; i++){
            int key = array[i];
            int j = i - gap;
            for(; j >= 0; j -= gap){
                if(array[j] > key){
                    array[j + gap] = array[j];
                }else{
                    break;
                }
            }
            array[j + gap] = key;
        }
    }
    public void shellSort(int[] array){
        int gap = array.length;
        while(gap > 1){
            gap >>>= 1;
            shell(array, gap);
        }
        shell(array, 1);
    }
    //选择排序
    //时间：固定不变O(n^2)
    //不稳定
    public void chooseSort(int[] array){
        for(int i = 0 ; i < array.length; i++){
            for(int j = i + 1; j < array.length; j++){
                if(array[i] > array[j]){
                    swap(array, i, j);
                }
            }
        }
    }
    /*public void swap(int[] array, int x, int y){
        int tmp = array[x];
        array[x] = array[y];
        array[y] = tmp;
    }*/
    //冒泡排序
    //时间：O(n^2),顺序O(n)
    //稳定
    public void bubble(int[] array){
        for(int i = 0; i < array.length - 1; i++){
            boolean flag = false;
            for(int j = 0; j < array.length - 1 - i; j++){
                if(array[j] > array[j + 1]){
                    swap(array, j, j + 1);
                    flag = true;
                }
            }
            if(!flag){
                break;
            }
        }
    }
    //堆排序
    //时间:O(n*logn)适用于各种环境

/*    public void heapSort(int[] array){
        createHeap(array);
        for(int end = array.length - 1 ; end > 0; end--){
            swap(array, 0, end);
            adjustDown(array, 0, end);
        }
    }
    //建立大根堆
    private void createHeap(int[] array){
        int len = array.length;
        for(int parent = (len - 1 - 1) / 2; parent >= 0; parent--){
            adjustDown(array, parent, len);
        }
    }
    private void adjustDown(int[] array, int parent, int len){
        int child = parent * 2 + 1;
        while(child < len){
            if(child + 1 < len && array[child] < array[child + 1]){
                child++;
            }
            if(array[parent] < array[child]){
                swap(array, parent, child);
                parent = child;
                child = parent * 2 + 1;
            }else{
                break;
            }
        }
    }*/
    //堆排序
    //时间:O(n*logn)
    //不稳定
    //建堆
/*    public void heapSort(int[] array){
        createHeap(array);
        for(int end = array.length - 1; end > 0; end--){
            swap(array, 0, end);
            adjustDown(array, 0, end);
        }
    }
    private void createHeap(int[] array){
        int len = array.length;
        for(int parent = (len - 1 - 1) / 2; parent >= 0; parent--){
            adjustDown(array, parent, len);
        }
    }
    private void adjustDown(int[] array, int parent, int len){
        int child = parent * 2 + 1;
        while(child < len){
            if(child + 1 < len && array[child] < array[child + 1]){
                child++;
            }
            if(array[parent] < array[child]){
                swap(array,parent, child);
                parent = child;
                child = parent * 2 + 1;
            }else{
                break;
            }
        }
    }*/
/*    public void heapSort(int[] array){
        createHeap(array);
        for(int end = array.length - 1; end > 0; end--){
            swap(array, 0, end);
            adjustDown(array, 0, end);
        }
    }
    private void createHeap(int[] array){
        int len = array.length;
        for(int parent = (len - 1 - 1) / 2; parent >= 0; parent--){
            adjustDown(array, parent, len);
        }
    }
    private void adjustDown(int[] array, int parent, int len){
        int child = parent * 2 + 1;
        while(child < len){
            if(child + 1 < len && array[child] > array[child + 1]){
                child++;
            }
            if(array[parent] > array[child]){
                swap(array, parent, child);
                parent = child;
                child = parent * 2 + 1;
            }else{
                break;
            }
        }
    }*/
    //快速排序
    //时间复杂度:O(n*logn);
    //空间：O(logn)
    //交换法
/*    private int fundPoint(int[] array, int start, int end){
        int key = array[start];
        while(start < end){
            while(start < end && key <= array[end]){
                end--;
            }
            swap(array, start, end);
            while(start < end && array[start] <= key){
                start++;
            }
            swap(array, start, end);
        }
        return start;
    }
    private void quick(int[] array, int left, int right){
        if(left >= right){
            return;
        }
        int point = fundPoint(array, left, right);
        quick(array, left, point - 1);
        quick(array, point + 1, right);
    }
    public void quickSort(int[] array){
        quick(array, 0, array.length - 1);
    }*/

   /* public void insertSort(int[] array, int start, int end){
        for(int i = start + 1; i <= end; i++){
            int key = array[i];
            int j = i - 1;
            for(; j >= 0; j--){
                if(array[j] > key){
                    array[j + 1] = array[j];
                }else{
                    break;
                }
            }
            array[j + 1] = key;
        }
    }*//*
    private int fundPoint2(int[] array, int start, int end){
        int key = array[start];
        while(start < end){
            while(start < end && key <= array[end]){
                end--;
            }
            array[start] = array[end];
            while(start < end && array[start] <= key){
                start++;
            }
            array[end] = array[start];
        }
        array[start] = key;
        return start;
    }
    private void quick2(int[] array, int left, int right){
        if(left >= right){
            return;
        }
        if(right - left + 1 <= 7){
            insertSort(array, left, right);
        }
        int point = fundPoint2(array, left, right);
        quick2(array, left, point - 1);
        quick2(array, point + 1, right);
    }
    public void quickSort2(int[] array){
        quick2(array, 0, array.length - 1);
    }*/
 /*   private int fundMid(int[] array , int left, int right){
        int mid = (left + right) / 2;
        if(array[left] > array[right]){
            if(array[mid] > array[left]){
                return left;
            }else if(array[right] > array[mid]){
                return right;
            }else{
                return mid;
            }
        }else{
            if(array[mid] < array[left]){
                return left;
            }else if(array[mid] > array[right]){
                return right;
            }else{
                return mid;
            }
        }
    }
    private int fundPoint(int[] array ,int start, int end){
        int key = array[start];
        while(start < end){
            while(start < end && key <= array[end]){
                end--;
            }
            array[start] = array[end];
            while(start < end && array[start] <= key){
                start++;
            }
            array[end] = array[start];
        }
        array[start] = key;
        return start;
    }
    private void quick(int[] array, int left, int right){
        if(left >= right){
            return;
        }
        //解决递归层次问题
        if(right - left + 1 <= 7){
            insertSort(array, left, right);
        }
        //三数取中
        int mid = fundMid(array, left, right);
        swap(array, left, mid);
        int point = fundPoint(array, left, right);
        quick(array, left, point - 1);
        quick(array, point + 1, right);
    }
    public void quickSort(int[] array){
        quick(array, 0, array.length - 1);
    }*//*
    private void insertSort(int[] array, int start, int end){
        for(int i = start + 1; i <= end; i++){
            int key = array[i];
            int j = i - 1;
            for(; j >= 0; j -= 1){
                if(array[j] > key){
                    array[j + 1] = array[j];
                }else{
                    break;
                }
            }
            array[j + 1] = key;
        }
    }
    private int fundMid(int[] array, int left, int right){
        int mid = (left + right) / 2;
        if(array[left] > array[right]){
            if(array[mid] > array[left]){
                return left;
            }else if(array[mid] < array[right]){
                return right;
            }else{
                return mid;
            }
        }else{
            if(array[mid] > array[right]){
                return right;
            }else if(array[mid] < array[left]){
                return left;
            }else{
                return mid;
            }
        }
    }
    private int fundPoint(int[] array, int start, int end){
        int key = array[start];
        while(start < end){
            while(start < end && key <= array[end]){
                end--;
            }
            array[start] = array[end];
            while(start < end && array[start] <= key){
                start++;
            }
            array[end] = array[start];
        }
        array[start] = key;
        return start;
    }
    private void quick(int[] array, int left, int right){
        if(left >= right){
            return;
        }
        //递归层次优化
        if(right - left <= 6){
            insertSort(array, left, right);
        }
        //三数取中优化
        int mid = fundMid(array, left, right);
        swap(array, left, mid);
        int point = fundPoint(array, left, right);
        quick(array, left, point - 1);
        quick(array, point + 1, right);
    }
    public void quickSort(int[] array){
        quick(array, 0, array.length - 1);
    }*/
/*    private int fundPoint(int[] array, int start, int end){
        int key = array[start];
        while(start < end){
            while(start < end && key <= array[end]){
                end--;
            }
            array[start] = array[end];
            while(start < end && array[start] <= key){
                start++;
            }
            array[end] = array[start];
        }
        array[start] = key;
        return start;
    }
    private int fundMid(int[] array, int left, int right){
        int midIndex = (left + right) / 2;
        if(array[left] > array[right]){
            if(array[midIndex] > array[left]){
                return left;
            }else if(array[midIndex] < array[right]){
                return right;
            }else{
                return midIndex;
            }
        }else{
            if(array[midIndex] > array[right]){
                return right;
            }else if(array[midIndex] < array[left]){
                return left;
            }else{
                return midIndex;
            }
        }
    }
    private void quick(int[] array, int left, int right){
        if(left >= right){
            return;
        }
        //三数取中
        int mid = fundMid(array, left, right);
        swap(array, mid, left);
        int point = fundPoint(array, left, right);
        quick(array, left, point - 1);
        quick(array, point + 1, right);
    }
    public void quickSort(int[] array){
        quick(array, 0, array.length - 1);
    }*/

    //归并排序
    //时间复杂度:真正的O(n*logn)
    //空间复杂度:O(n)
    //稳定
/*    private void megerFunc(int[] array, int left, int right){
        if(left >= right){
            return;
        }
        int mid = (left + right) / 2;
        megerFunc(array, left, mid);
        megerFunc(array, mid + 1, right);
        meger(array, left, mid, right);
    }// 1 2 3 4 5
    private void meger(int[] array, int start, int mid, int end){
        int[] tmpArray = new int[end - start + 1];
        int k = 0;
        int s1 = start;
        int s2 = mid + 1;
        while(s1 <= mid && s2 <= end){
            if(array[s1] < array[s2]){
                tmpArray[k++] = array[s1++];
            }else{
                tmpArray[k++] = array[s2++];
            }
        }
        while(s1 <= mid){
            tmpArray[k++] = array[s1++];
        }
        while(s2 <= end){
            tmpArray[k++] = array[s2++];
        }
        for(int i = 0; i < tmpArray.length; i++){
            array[i + start] = tmpArray[i];
        }
    }
    public void mergeSort(int[] array){
        megerFunc(array, 0, array.length - 1);
    }*/
    //归并排序
    //时间复杂度:O(n*logn)
    //空间复杂度O(n)
    //稳定
    private void megerFunc(int[] array, int left, int right){
        if(left == right){
            return;
        }
        int mid = (left + right) / 2;
        megerFunc(array, left, mid);
        megerFunc(array, mid + 1, right);
        meger(array, left, mid, right);
    }
    private void meger(int[] array, int start, int mid, int end){
        int[] tmpArray = new int[end - start + 1];
        int k = 0;
        int s1 = start;
        int s2 = mid + 1;
        while(s1 <= mid && s2 <= end){
            if(array[s1] < array[s2]){
                tmpArray[k++] = array[s1++];
            }else{
                tmpArray[k++] = array[s2++];
            }
        }
        while(s1 <= mid){
            tmpArray[k++] = array[s1++];
        }
        while(s2 <= end){
            tmpArray[k++] = array[s2++];
        }
        //合并
        for(int i = 0; i < tmpArray.length; i++){
            array[i + start] = tmpArray[i];
        }
    }
    public void mergeSort(int[] array){
        megerFunc(array, 0, array.length - 1);
    }

    //堆排序
    //时间复杂度:O(n*logn)
    //空间复杂度:O(1)
    //不稳定
    public void heapSort(int[] array){
        createBigHeap(array);
        for(int end = array.length - 1; end > 0; end--){
            swap(array, 0, end);
            adjustDown(array, 0, end);
        }
    }
    //创捷大根堆
    public void createBigHeap(int[] array){
        int len = array.length;
        for(int parent = (len - 1 - 1) / 2; parent >= 0; parent--){
            adjustDown(array, parent, len);
        }
    }
    //向下调整
    public void adjustDown(int[] array, int parent, int len){
        int child = parent * 2 + 1;
        while(child < len){
            if(child + 1 < len && array[child] < array[child + 1]){
                child++;
            }
            if(array[parent] < array[child]){
                swap(array, parent, child);
                parent = child;
                child = parent * 2 + 1;
            }else{
                break;
            }
        }
    }
    //交换
    public void swap(int[] array, int i, int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    //快速排序
    //挖坑找基准
    public int fundPoint(int[] array, int start, int end){
        int key = array[start];
        while(start < end){
            while(start < end && array[end] >= key){
                end--;
            }
            array[start] = array[end];
            while(start < end && array[start] <= key){
                start++;
            }
            array[end] = array[start];
        }
        array[start] = key;
        return start;
    }
    //三数取中优化
    public int fundMid(int[] array, int left, int right){
        int mid = (left + right) / 2;
        if(array[left] < array[right]) {
            if (array[mid] < array[left]) {
                return left;
            } else if (array[mid] > array[right]) {
                return right;
            } else {
                return mid;
            }
        }else{
            if(array[left] < array[mid]){
                return left;
            }else if(array[right] > array[mid]){
                return right;
            }else{
                return mid;
            }
        }
    }
    public void quick(int[] array, int left, int right){
        if(left >= right){
            return;
        }
        //三数取中优化
        int mid = fundMid(array, left, right);
        swap(array, left, mid);
        int point = fundPoint(array, left, right);
        quick(array, left, point - 1);
        quick(array, point + 1, right);
    }
    public void quickSort(int[] array){
        quick(array, 0, array.length - 1);
    }



}
